package com.texthtml;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.app.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class MainActivity extends Activity {

	private String textHtml = "Click button to load url";
	
	@ViewInject(R.id.textView1)
    private TextView textView1;
    
    @ViewInject(R.id.button1)
    private Button button1;
	
	public static final int UPDATE_TEXT = 1;	
	private Handler Handler = new Handler(){
		public void handleMessage(Message msg){
			switch(msg.what){
				case UPDATE_TEXT:
					// update UI					
					Spanned spanned = Html.fromHtml(textHtml, new MyImageGetter(MainActivity.this,textView1), null);		
					textView1.setText(spanned);		
					textView1.setMovementMethod(LinkMovementMethod.getInstance());					
					break;
				default:
					break;
			}
		}
	};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_texthtml);
		com.lidroid.xutils.ViewUtils.inject(this);	
		Spanned spanned = Html.fromHtml(textHtml, new MyImageGetter(MainActivity.this,textView1), null);		
		textView1.setText(spanned);		
		textView1.setMovementMethod(LinkMovementMethod.getInstance());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	// 异步更新UI
		@OnClick(R.id.button1)
	    public void testBtnClick(View v) {
			new Thread(new Runnable(){
				@Override
				public void run(){
					
					// 获取html内容
					RequestParams params = new RequestParams();
					
					// 把要发送的内容编码为json
					/*
					HashMap<String, String> hm_kv = new <String, String>HashMap();
					hm_kv.put("item1", "小狗");
					hm_kv.put("item2","cat");
					hm_kv.put("item3", "大猩猩");
					hm_kv.put("item4", "donkey");	
					String json_str = FastJson.object2JsonString(hm_kv);
					params.addBodyParameter("jstr", json_str);
					*/					
					
					HttpUtils http = new HttpUtils();
					http.send(HttpRequest.HttpMethod.POST,"http://image.baidu.com/",
					    params,
					    new RequestCallBack<String>() {

					        @Override
					        public void onStart() {
					        	textView1.setText("conn...");
					        }

					        @Override
					        public void onLoading(long total, long current, boolean isUploading) {
					            if (isUploading) {
					            	textView1.setText("upload: " + current + "/" + total);
					            } else {
					            	textView1.setText("reply: " + current + "/" + total);
					            }
					        }

					        @Override
					        public void onSuccess(ResponseInfo<String> responseInfo) {
					        	/*        	
					        	String json_str =(String) responseInfo.result;
					        	HashMap<String, String> hm =FastJson.jsonString2Object(json_str, HashMap.class);
					        	textView1.setText("reply: " + hm.get("name"));
					        	*/
					        	
					        	String html =(String) responseInfo.result;
					        	textHtml = html;
					        	Message message = new Message();
								message.what = UPDATE_TEXT;
								Handler.sendMessage(message);  // sent message
					        }

					        @Override
					        public void onFailure(HttpException error, String msg) {
					        	textView1.setText(error.getExceptionCode() + ":" + msg);
					        }
					});	
					
				}
			}).start();	
	    }	
}
